package com.aizuda.boot.modules.business.contract.controller;


import com.aizuda.boot.modules.business.contract.entity.dto.ContractTypeAddDTO;
import com.aizuda.boot.modules.business.contract.entity.dto.ContractTypeSortDTO;
import com.aizuda.boot.modules.business.contract.entity.dto.ContractTypeUpdateDTO;
import com.aizuda.boot.modules.business.contract.dto.SingleResponse;
import com.aizuda.boot.modules.business.contract.entity.vo.ContractTypeDetailShowDTO;
import com.aizuda.boot.modules.business.contract.entity.vo.ContractTypeShowDTO;
import com.aizuda.boot.modules.business.contract.entity.vo.ContractTypeTree;
import com.aizuda.boot.modules.business.contract.service.ContractService;
import com.aizuda.boot.modules.business.contract.service.impl.ContractExe;
import com.aizuda.boot.modules.business.contract.entity.ContractTypeEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author sangyirong
 * @ClassName ContractTypeConyroller
 * @Version 1.0
 * @Date 2025/5/29 17:57
 */
@RestController
@RequestMapping("/contract/types")
@Tag(name = "合同类型管理接口")
public class ContractTypeController {

    @Autowired
    private ContractExe contractExe;


    @Operation(summary = "获取合同类型列表")
    @GetMapping
    public SingleResponse<List<ContractTypeShowDTO>> listContractTypes() {
        return contractExe.listContractTypes();
    }

    @Operation(summary = "根据权限获取全部合同类型（层级列表）")
    @GetMapping("/permission")
    public SingleResponse<List<ContractTypeEntity>> getAllContractTypesByPermission() {
        return contractExe.getAllContractTypesByPermission();
    }

    @Operation(summary = "创建合同类型")
    @PostMapping
    public SingleResponse createContractType(@RequestBody ContractTypeAddDTO contractTypeAddDTO) {
        return contractExe.createContractType(contractTypeAddDTO);
    }

    @Operation(summary = "获取合同类型详情")
    @GetMapping("/{id}")
    public SingleResponse<ContractTypeDetailShowDTO> getContractType(@PathVariable Integer id) {
        return contractExe.getContractType(id);
    }

    @Operation(summary = "编辑合同类型")
    @PutMapping("/{id}")
    public SingleResponse updateContractType(@PathVariable Integer id, @RequestBody ContractTypeUpdateDTO contractTypeUpdateDTO) {
        return contractExe.updateContractType(id, contractTypeUpdateDTO);
    }

    @Operation(summary = "启用/禁用合同类型")
    @PutMapping("/{id}/status")
    public SingleResponse toggleContractTypeStatus(@PathVariable Integer id) {
        return contractExe.toggleContractTypeStatus(id);
    }

    @Operation(summary = "删除合同类型")
    @DeleteMapping("/{id}")
    public SingleResponse deleteContractType(@PathVariable Integer id) {
        return contractExe.deleteContractType(id);
    }

    @Operation(summary = "合同类型排序")
    @PutMapping("/sort")
    public SingleResponse sortContractTypes(@RequestBody ContractTypeSortDTO contractTypeSortDTO){
         return contractExe.sortContractTypes(contractTypeSortDTO);
    }

    @Operation(summary = "获取一级二级合同类型树")
    @GetMapping("/contractTypeTree")
    public SingleResponse<List<ContractTypeTree>> getContractTypeTree(){
        return contractExe.getContractTypeTree();
    }

    @Operation(summary = "根据名称模糊搜索合同类型", description = "根据合同类型名称进行模糊搜索，如果搜索到一级类型会返回其完整的子类型层级，避免重复")
    @GetMapping("/search")
    public SingleResponse<List<ContractTypeShowDTO>> searchContractTypes(
            @RequestParam(value = "typeName", required = true) String typeName) {
        return contractExe.searchContractTypesByName(typeName);
    }


    @Operation(summary = "获取长期合同类型树")
    @GetMapping("/longTermTree")
    public SingleResponse<List<ContractTypeTree>> getLongTermContractTypeTree(){
        return contractExe.getLongTermContractTypeTree();
    }
}
